package edu.scau.web.pos;

import com.alipay.api.AlipayApiException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.core.constants.AjaxResult;
import edu.scau.core.exection.BusinessProcessException;
import edu.scau.pos.service.OrderManageService;
import edu.scau.pos.service.OrdersService;
import edu.scau.pos.vo.CategoryVo;
import edu.scau.pos.vo.OrdersVo;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/orderManager")
@Tag(name = "订单管理业务")
public class OrderManagerController {
    @Autowired
    private OrderManageService orderManageService;
    @Autowired
    private OrdersService ordersService;

    @Operation(
            summary = "查找所有订单",
            description = "查找所有订单，访问权限:admin、cashier",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = OrdersVo.class))),

            }
    )
    @GetMapping("/backorderShipment")
    @PreAuthorize("hasAnyAuthority('admin','cashier')")
    public AjaxResult listAll () {
        List<OrdersVo> ordersList = ordersService.listAll();
        return AjaxResult.success(ordersList);
    }

    @Operation(
            summary = "查找所有缺货订单",
            description = "分页查找查找所有缺货订单,访问权限admin、cashier",
            responses = {
                    @ApiResponse(content = @Content(schema=@Schema(implementation = OrdersVo.class))),
            }
    )
    @GetMapping("/listAllBlock")
    public AjaxResult listAllBlock(@RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrdersVo> ordersList = ordersService.listAllBlock();
        PageInfo<OrdersVo> pageInfo = new PageInfo<>(ordersList);
        return AjaxResult.success(pageInfo);
    }

    @Operation(
            summary = "缺货补发",
            description = "对挂单订单进行补发，访问权限:admin、cashier",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = CategoryVo.class))),

            }
    )
    @Hidden
    @PostMapping("/backorderShipment")
    @PreAuthorize("hasAnyAuthority('admin','cashier')")
    public AjaxResult backorderShipment (@Validated  @RequestBody OrdersVo ordersVo) {
            Map data=orderManageService.backorderShipment(ordersVo);
            return AjaxResult.success(data);

    }

    @Operation(
            summary = "通过ID缺货补发",
            description = "对挂单订单进行补发，访问权限:admin、cashier",
            parameters = {
                    @Parameter(name = "orderId",description = "补发订单ID",required = true)
            },
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = CategoryVo.class))),

            }
    )
    @GetMapping("/backorderShipmentByOrderId")
    @PreAuthorize("hasAnyAuthority('admin','cashier')")
    public AjaxResult backorderShipmentByOrderId (@RequestParam("orderId") String ordersId) {
            Map data=orderManageService.backorderShipmentByOrderId(ordersId);
            return AjaxResult.success(data);
    }



}
